package com.zetor.ms.facade.controller;

import com.zetor.ms.application.service.base.ITransactionService;
import com.zetor.ms.core.bo.Result;
import com.zetor.ms.core.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 事务 demo controller
 *
 * @author zlc
 * @date 6.30
 */
@RestController
public class TransactionController extends BaseController {
    @Autowired
    private ITransactionService tranService;

    /**
     * 分布式事务 seata， 两服务
     * case1: client a -> client b， 成功commit
     * case2: client a -> client b，client a发生异常
     * case3: client a -> client b，client b发生异常
     * 全局事务rollback
     * @return
     */
    @RequestMapping(value = "/v1/seata/clienta/caseOne", method = RequestMethod.POST)
    public Result testSeataCaseOne() {
        return result(tranService.seataCaseOne());
    }


    /**
     * 分布式事务 seata, 三服务
     * case4: client a -> client b -> client c，client c发生exception
     * case5: client a -> client b， client a -> client c，client c发生exception
     * 全局事务rollback
     * @return
     */
    @RequestMapping(value = "/v1/seata/clienta/caseTwo", method = RequestMethod.POST)
    public Result testSeataCaseTwo() {
        return result(tranService.seataCaseTwo());
    }


    /**
     * 分布式事务 seata，模拟业务
     * 全局事务rollback
     * @return
     */
    @RequestMapping(value = "/v1/seata/clienta/caseThree", method = RequestMethod.POST)
    public Result testSeataCaseThree() {
        return result(tranService.seataCaseThree());
    }

}
